package org.manager.generic.web;

public class Rule {

	protected String includeProperty[] = new String[0];
	protected String excludeProperty[] = new String[0];
	protected String systemIncludeProperty[] = new String[0];
	                                                            //{JsonViewUtil.DATA_NAME,JsonViewUtil.SUCCESS,JsonViewUtil.FAILURE,JsonViewUtil.Err_MSG};
	protected final String systemExcludeProperty[] = new String[]{JsonViewUtil.JSON_CONF};
	
	private boolean defultExclude = true;
	
	public Rule(){}
	
	public void setSystemIncludeProperty(String property[]){
		systemIncludeProperty = property;
	}
	
	public void setDefultExclude(boolean is){
		defultExclude = is;
	}
	
	public boolean isDefultExclude(){
		return defultExclude;
	}
	
	public void setIncludePropertys(String propertys[]){
		this.includeProperty = propertys;
	}
	
	public void setExcludeProperty(String propertys[]){
		this.excludeProperty = propertys;
		this.defultExclude = false;
	}
	
	public boolean isInclude(String propertyName){
		if (propertyName == null || propertyName.equalsIgnoreCase("null")) return true;
		return exist(includeProperty, propertyName) || exist(systemIncludeProperty, propertyName);
	}
	
	public boolean isExclude(String propertyName){
		if (propertyName == null || propertyName.equalsIgnoreCase("null")) return false;
		return exist(excludeProperty, propertyName) || exist(systemExcludeProperty, propertyName);
	}
	
	private boolean exist(String array[], String propertyName){
		if (array == null || array.length < 1) return false;
		if (propertyName == null || propertyName.equalsIgnoreCase("null")) return false;
		for(int i=0; i<array.length; i++){
			if(propertyName.equalsIgnoreCase(array[i])){
				return true;
			}
		}
		return false;
	}
	
	
	@Override
	public String toString() {
		return Rule.class+": {includeProperty: ["+array2String(includeProperty)+"], excludeProperty: ["+array2String(excludeProperty)+"]}";
	}
	
	private String array2String(String[] array){
		if(array == null) return "";
		StringBuffer result = new StringBuffer();
		for(int i=0; i<array.length; i++){
			result.append(array[i]+",");
		}
		return result.toString();
	}
	
}
